﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ChimneySim
{
    public class Billboard
    {
        private Texture2D texture;

        private Vector2 size;

        public Vector2 Size
        {
            get { return size; }
            set { size = value; }
        }

        private GraphicsDevice gd;

        private Vector3 position;

        private VertexPositionNormalTexture[] vertices;
        private int[] indices;

        private Effect effect;

        public Billboard(Vector3 position, Texture2D texture, Vector2 size, Effect billboardEffect, GraphicsDevice gd)
        {
            this.texture = texture;
            this.size = size;
            this.effect = billboardEffect;
            this.gd = gd;
            this.position = position;
            
            GenerateBillboard();
        }

        private void GenerateBillboard()
        {
            vertices = new VertexPositionNormalTexture[4];
            indices = new int[6];

            float width = size.X;
            float height = size.Y;

            vertices[0] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Up, new Vector2(0, 1));
            vertices[1] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Up, new Vector2(1, 1));
            vertices[2] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Up, new Vector2(1, 0));
            vertices[3] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Up, new Vector2(0, 0));

            indices[0] = 3;
            indices[1] = 2;
            indices[2] = 0;

            indices[3] = 2;
            indices[4] = 1;
            indices[5] = 0;

        }

        public void Draw(Matrix View, Matrix Projection)
        {
            Matrix World = Matrix.Identity * Matrix.CreateTranslation(position);

            effect.Parameters["Texture"].SetValue(texture);
            effect.Parameters["World"].SetValue(World);
            effect.Parameters["View"].SetValue(View);
            effect.Parameters["Projection"].SetValue(Projection);
            effect.Parameters["size"].SetValue(size);

            gd.BlendState = BlendState.AlphaBlend;

            //First pass
            gd.DepthStencilState = DepthStencilState.Default;
            effect.Parameters["AlphaTest"].SetValue(true);
            effect.Parameters["AlphaTestGreater"].SetValue(true);
            effect.CurrentTechnique.Passes[0].Apply();
            gd.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);

            gd.DepthStencilState = DepthStencilState.DepthRead;
            effect.Parameters["AlphaTest"].SetValue(true);
            effect.Parameters["AlphaTestGreater"].SetValue(false);
            effect.CurrentTechnique.Passes[0].Apply();
            gd.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);

            gd.DepthStencilState = DepthStencilState.Default;
            gd.BlendState = BlendState.Opaque;            
        }
    }
}
